package com.argrace.smart.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.alink.business.alink.ALinkBusinessEx;
import com.aliyun.alink.sdk.net.anet.api.AError;
import com.aliyun.alink.sdk.net.anet.api.transitorynet.TransitoryRequest;
import com.aliyun.alink.sdk.net.anet.api.transitorynet.TransitoryResponse;
import com.argrace.smart.R;
import com.argrace.smart.activity.devicerelation.DeviceRelation_KaiGuanActivity;
import com.argrace.smart.base.BaseActivity;
import com.argrace.smart.base.ConstantConfigure;
import com.argrace.smart.entity.Key_ValueEntity;
import com.argrace.smart.entity.MineZigBeeDeviceListEntity;
import com.argrace.smart.entity.WangGuanDeviceEntity;
import com.argrace.smart.glide.GliderImageLoaderUtil;
import com.argrace.smart.model.LocalDb_AnJianTable;
import com.argrace.smart.model.LocalDb_DeviceSyncTable;
import com.argrace.smart.model.LocalDb_QingJingTable;
import com.argrace.smart.model.LocalDb_RelationDeviceTable;
import com.argrace.smart.ui.DrawableSwitch;
import com.argrace.smart.util.AlinkHttpUtil;

import org.litepal.crud.DataSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018\3\15 0015.
 */

public class DeviceSync_KaiGuanActivity extends BaseActivity {
    private ImageView img;
    private TextView text1, text2, text3, text4, text5, text6;
    MineZigBeeDeviceListEntity bean;
    private LinearLayout ll1, ll2, ll3, ll4, ll5, ll6;
    private Map<String, String> model_imgurl = new HashMap<>();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_devicecontrol_fukong);
        init();
    }

    private void init() {
        bean = (MineZigBeeDeviceListEntity) getIntent().getSerializableExtra("deviceinfo");
        setTitle(true, false, bean.getName());
        showRight(true, R.mipmap.head_title_tick);
        setLeftandRightClickListeners(null, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                save();
            }
        });
        setView();
        getDeviceDetailInfo();
    }

    private void save() {

    }

    private void getMyDevice(final int requestCode) {
        String method = "mtop.openalink.app.core.devices.getbyuser";
        AlinkHttpUtil.AlinkHttpRequest(method, new HashMap<String, Object>(), new ALinkBusinessEx.IListener() {
            @Override
            public void onSuccess(TransitoryRequest transitoryRequest, TransitoryResponse transitoryResponse) {
                if (transitoryResponse != null && transitoryResponse.data != null) {
                    List<WangGuanDeviceEntity> beans = JSON.parseArray((String) transitoryResponse.data, WangGuanDeviceEntity.class);
                    if (beans != null && beans.size() > 0) {
                        Intent intent = new Intent(DeviceSync_KaiGuanActivity.this, RuoQi_SubDeviceListActivity.class);
                        intent.putExtra("uuid", beans.get(0).getUuid());
                        startActivityForResult(intent, requestCode);
                    }
                }

            }

            @Override
            public void onFailed(TransitoryRequest transitoryRequest, AError aError) {
            }
        });
    }

    private void setView() {
        img = (ImageView) findViewById(R.id.activity_devicecontrol_img);
        text1 = (TextView) findViewById(R.id.activity_devicecontrol_fukong_text1);
        text2 = (TextView) findViewById(R.id.activity_devicecontrol_fukong_text2);
        text3 = (TextView) findViewById(R.id.activity_devicecontrol_fukong_text3);
        text4 = (TextView) findViewById(R.id.activity_devicecontrol_fukong_text4);
        text5 = (TextView) findViewById(R.id.activity_devicecontrol_fukong_text5);
        text6 = (TextView) findViewById(R.id.activity_devicecontrol_fukong_text6);
        ll1 = (LinearLayout) findViewById(R.id.activity_devicecontrol_fukong_ll1);
        ll2 = (LinearLayout) findViewById(R.id.activity_devicecontrol_fukong_ll2);
        ll3 = (LinearLayout) findViewById(R.id.activity_devicecontrol_fukong_ll3);
        ll4 = (LinearLayout) findViewById(R.id.activity_devicecontrol_fukong_ll4);
        ll5 = (LinearLayout) findViewById(R.id.activity_devicecontrol_fukong_ll5);
        ll6 = (LinearLayout) findViewById(R.id.activity_devicecontrol_fukong_ll6);
        String imgurl = null;
        if (bean.getModel().equals(ConstantConfigure.MODEL_SANWEIDUOKONG)) {
            imgurl = ConstantConfigure.IMGURL_SANWEIQINGCHUFUKONG_005F1E14;
            ll1.setVisibility(View.VISIBLE);
            ll2.setVisibility(View.VISIBLE);
            ll3.setVisibility(View.VISIBLE);
            ll4.setVisibility(View.GONE);
            ll5.setVisibility(View.GONE);
            ll6.setVisibility(View.GONE);
            ll1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getMyDevice(ConstantConfigure.REQUESTCODE_SWITCH1);
                }
            });
            ll2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getMyDevice(ConstantConfigure.REQUESTCODE_SWITCH2);
                }
            });
            ll3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getMyDevice(ConstantConfigure.REQUESTCODE_SWITCH3);
                }
            });
        }
        if (bean.getModel().equals(ConstantConfigure.MODEL_LIANGWEIDUOKONG)) {
            imgurl = ConstantConfigure.IMGURL_LIANGWEIQINGCHUFUKONG_005f1dcc;
            ll1.setVisibility(View.VISIBLE);
            ll2.setVisibility(View.VISIBLE);
            ll3.setVisibility(View.GONE);
            ll4.setVisibility(View.GONE);
            ll5.setVisibility(View.GONE);
            ll6.setVisibility(View.GONE);
            ll1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getMyDevice(ConstantConfigure.REQUESTCODE_SWITCH1);
                }
            });
            ll2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getMyDevice(ConstantConfigure.REQUESTCODE_SWITCH2);
                }
            });
        }
        if (bean.getModel().equals(ConstantConfigure.MODEL_YIWEIDUOKONG)) {
            imgurl = ConstantConfigure.IMGURL_YIWEIQINGCHUFUKONG_005F1E12;
            ll1.setVisibility(View.VISIBLE);
            ll2.setVisibility(View.GONE);
            ll3.setVisibility(View.GONE);
            ll4.setVisibility(View.GONE);
            ll5.setVisibility(View.GONE);
            ll6.setVisibility(View.GONE);
            ll1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getMyDevice(ConstantConfigure.REQUESTCODE_SWITCH1);
                }
            });
        }

        if (imgurl != null) {
            GliderImageLoaderUtil.load(this, imgurl, img);
        }
    }

    //查询设备存储的场景信息
    private void getDeviceDetailInfo() {

        List<LocalDb_AnJianTable> beans = DataSupport.where("uuid = ?", bean.getUuid()).find(LocalDb_AnJianTable.class);
        if (beans != null && beans.size() > 0) {
            //如果有数据，查询情景表
            for (int i = 0; i < beans.size(); i++) {
                LocalDb_QingJingTable bean = DataSupport.find(LocalDb_QingJingTable.class, beans.get(i).getQingJingTable_id());
                fillData(beans.get(i), bean);
            }
        }
    }

    private void fillData(LocalDb_AnJianTable bean_anjian, LocalDb_QingJingTable bean_qingjing) {
        switch (bean_anjian.getKey()) {
            case "1":
                text1.setText(bean_qingjing.getName());
                break;
            case "2":
                text2.setText(bean_qingjing.getName());
                break;
            case "3":
                text3.setText(bean_qingjing.getName());
                break;
            case "4":
                text4.setText(bean_qingjing.getName());
                break;
            case "5":
                text5.setText(bean_qingjing.getName());
                break;
            case "6":
                text6.setText(bean_qingjing.getName());
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            String zhushebeijianzhi = null;
            String result = data.getStringExtra("result");
            String uuidAndproperty = null;
            String fushebeiuuid = null;
            String fushebeijianzhi = null;
            String[] relationdevice_arr = result.split(":");
            if (relationdevice_arr != null && relationdevice_arr.length == 2) {
                String[] relationdevice_uuidandproperty_arr = relationdevice_arr[1].split("-");
                if (relationdevice_uuidandproperty_arr != null && relationdevice_uuidandproperty_arr.length == 2) {
                    fushebeiuuid = relationdevice_uuidandproperty_arr[0];
                    fushebeijianzhi = relationdevice_uuidandproperty_arr[1];
                } else if (relationdevice_uuidandproperty_arr != null && relationdevice_uuidandproperty_arr.length == 1) {
                    fushebeiuuid = relationdevice_uuidandproperty_arr[0];
                    fushebeijianzhi = "Switch";
                }
            }
            if (requestCode == ConstantConfigure.REQUESTCODE_SWITCH1) {
                if (bean.getModel().equals(ConstantConfigure.MODEL_YIWEIDUOKONG)) {
                    zhushebeijianzhi = "Switch";
                } else {
                    zhushebeijianzhi = "Switch_1";
                }
                text1.setText(result);
            }
            if (requestCode == ConstantConfigure.REQUESTCODE_SWITCH2) {
                zhushebeijianzhi = "Switch_2";
                text2.setText(result);
            }
            if (requestCode == ConstantConfigure.REQUESTCODE_SWITCH3) {
                zhushebeijianzhi = "Switch_3";
                text3.setText(result);
            }
            if (fushebeiuuid != null) {
                LocalDb_DeviceSyncTable entity = new LocalDb_DeviceSyncTable();
                entity.setZhusheibeiuuid(bean.getUuid());
                entity.setZhushebeijianzhi(zhushebeijianzhi);
                entity.setFushebeiuuid(fushebeiuuid);
                if (fushebeijianzhi == null) {
                    entity.setFushebejianzhi("Switch");
                }
                else
                {
                    entity.setFushebejianzhi(fushebeijianzhi);
                }
                int db_result = entity.updateAll("zhusheibeiuuid = ? and zhushebeijianzhi = ? ", entity.getZhusheibeiuuid(), entity.getZhushebeijianzhi());
                if (db_result == 0) {
                    entity.save();
                }

            }
        }

    }
}
